$(document).ready(function(){
    
    $('#signin_submit').click(function(e){
        e.preventDefault();
        var userEmail = $('#signin_form_mail_input').val();
        var userPassword = $('#signin_form_password_input').val();
        
        login(userEmail, userPassword);
    })

    $('#signup_submit').click(function(e){
        e.preventDefault();
        var userNickname = $('#signup_form_nickname_input').val();
        var userEmail = $('#signup_form_mail_input').val();
        var userPassword = $('#signup_form_password_input').val();
        var userConfirmPassword = $('#signup_form_confirm_password_input').val();
        
        register(userNickname, userEmail, userPassword, userConfirmPassword)
    })
})

function login(userEmail, userPassword) {
    Common.Api.callJson
    (
        null,
        'User',
        'signIn',
        {
            userEmail: userEmail,
            userPassword: userPassword
        },
        function(result){
            location.href = result.urlOfRedirection
        }
    )
}

function register(userNickname, userEmail, userPassword, userConfirmPassword) {
    Common.Api.callJson
    (
        null,
        'User',
        'register',
        {
            userNickname: userNickname,
            userEmail: userEmail,
            userPassword: userPassword,
            userConfirmPassword: userConfirmPassword
        },
        function(){
            login(userEmail, userPassword);
        }
    )
}